if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}

*** LATE estimates

clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1
drop if replacement == 1 & gender == 1

*Define strata
egen strata=group(gender state_old)

*Generate treatment streams
gen treatmentX=0
replace treatmentX=1 if treatment_stream==3

ren edu_level_b  edu_b

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b

* Generate sum of interaction and level
gen TplusD = treatment + lg_kcb_dist 
gen TtimesDminus1 = treatment * (lg_kcb_dist - 1)
gen TplusNogrant = treatment + treatmentX
gen TtimesNOgrantminus1 = treatment * (treatmentX - 1)

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict

global base_controls i.edu_b i.treat_edu i.num_eval_b i.treat_num i.lit_eval_b i.treat_lit

***Socioeconomic Survey outcomes ******************************************************************
clear matrix
mat A = J(9,6,.)
global n 1

foreach var of varlist employment_index consumption_index savings_debt_index bizskills_index psycho_index risk_index trust_index crime_index migration_index {

	
	eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  i.strata ( treatmentX= Z_treat_kcb) , cluster(boma_old) savefprefix(fstage_) first savefirst
		local t_alpha = invttail(`e(Fdf2)', 0.05/2)
		display `t_alpha'
		local se = _se[treatmentX]
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 1] = `MDE'		
		local se = _se[treatment]
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 2] = `MDE'
		
		lincom treatmentX + treatment
		local se = `r(se)'
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 3] = `MDE'	

	 eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls $base_controls  i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
	local t_alpha = invttail(`e(Fdf2)', 0.05/2)
		display `t_alpha'
		local se = _se[treatmentX]
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 4] = `MDE'		
		local se = _se[treatment]
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 5] = `MDE'	
		
		lincom treatmentX + treatment
		local se = `r(se)'
		display `se'
		local MDE = `se' * `t_alpha'
		display `MDE'
		mat A[$n, 6] = `MDE'	
		
		global n = $n + 1
}

matrix rownames A = "Employment index" "Consumption index" "Savings index" "Business skills index" "Psychological wellbeing index" "Risk index" "Trust index" "Crime and violence index" "Migration index"
frmttable , statmat(A) replace sdec(3) title (Table G.2: \emph{Ex post} minimum detectable effect size: LATE estimates) 
frmttable using "$gsdTables/TableA.7.tex", replay replace tex

